package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 新增课程界面
 *
 * @author yingchunli
 */
public class AddCourseActivity extends BaseSoocActivity {

    @InjectView(R.id.courseName_addcourse)
    ClearEditText courseName;//课程名称
    @InjectView(R.id.courseTime_addcourse)
    TextView courseTime;//上课学期
    @InjectView(R.id.commit_addcourse)
    Button commitBtn;//提交按钮
    CallNameDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_addcourse);
        ButterKnife.inject(this);
        dao = new CallNameDao(this);
        commitBtn.setEnabled(false);//设置提交按钮初始状态不能点击
        dao.getNowXueqi(CodeConst.EXPAND_QUQXI_GET_XQ);

        //按键完成监听
        courseName.setImeOptions(EditorInfo.IME_ACTION_DONE);
        courseName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    onClick();
                }
                return false;
            }
        });
    }

    /**
     * 判断必填内容是否为空，确定注册按钮处于什么状态
     * 当必填内容全不为空时，提交 按钮才处于可点击状态，否则，不可点击
     */
    @OnTextChanged(value = {R.id.courseName_addcourse, R.id.courseTime_addcourse},
            callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (courseName.getText().toString().trim().equals("") || courseTime.getText().toString().trim().equals("")) {
            commitBtn.setEnabled(false);
        } else {
            commitBtn.setEnabled(true);
        }
    }

    /**
     * 提交按钮的点击
     */
    @OnClick(R.id.commit_addcourse)
    void onClick() {
        String kcmc = courseName.getText().toString().trim();
        String skxq = courseTime.getText().toString().trim();
        if (kcmc.equals("")) {
            MessageUtils.showImageShortToast(this, "课程名称不能空");
        } else {
            dao.createCourse(kcmc, CodeConst.EXPAND_CREATE_COURSE);
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            switch (requestCode) {
                case CodeConst.EXPAND_CREATE_COURSE://创建课程成功
                    if (dao.getStatus().equals("0")) {
                        MessageUtils.showRightImageShortToast(this, "创建课程成功");
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        MessageUtils.showImageShortToast(this, dao.getStatusCode());
                    }
                    break;
                case CodeConst.EXPAND_QUQXI_GET_XQ://获取上课学期
                    courseTime.setText(dao.getSkxq());
                    break;
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "新增课程";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @Override
    public boolean setupToolBarRightButton(ImageView rightButton) {
        return false;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeft() {
        finish();
    }

}
